<?php 
ini_set('memory_limit', '5120M');
ini_set('max_execution_time', '600');
ini_set("gd.jpeg_ignore_warning", 1);


    function compress_image($source_url, $destination_url, $quality) {
	//define max size
	$max_size = 225280;
	
	$info = getimagesize($source_url);
 
	if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($source_url);
	elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($source_url);
	elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($source_url);
	$imgsize=filesize($source_url);
	
 	if($imgsize>$max_size){
		//save file
		$quality=ceil(($max_size/$imgsize)*100);
		$quality=100-$quality;
		if ($quality<90) {
			imagejpeg($image, $destination_url, $quality);
		}
 	}
	//return destination file
	return $destination_url;
}

class image_resize_thumb
{
var $set_img_thumb_max_width;
var $set_img_thumb_max_height;
var $img_new_thumb_width;
var $img_new_thumb_height;
var $mime;
var $image;
var $img_thumb_width;
var $img_thumb_height;
var $img_path;
var $img_thumb_save_path;
var $img;
 
		function image_file($img_file,$img_thumbnail){
           $img_type = array("jpg", "jpeg", "png", "gif");
 
                 $ext = pathinfo($img_file, PATHINFO_EXTENSION);
                 if (in_array($ext, $img_type)) {
                     $this->img_path = $img_file;
                     $this->img_thumb_save_path = $img_thumbnail;
                     $this->img_resize_to_thumb();
 
                 }
         
 
   		 }

    function image_file_path($img_file_path,$img_thumbnail_path){
           $img_type = array("jpg", "jpeg", "png", "gif");
           $files = scandir($img_file_path);
            array_shift($files);
            array_shift($files);
            foreach($files as $key=>$value){
 
                 $ext = pathinfo($value, PATHINFO_EXTENSION);
                 if (in_array($ext, $img_type)) {
 
                     $this->img_path = $img_file_path.$value;
                     $this->img_thumb_save_path = $img_thumbnail_path.$value;
                     $this->img_resize_to_thumb();
 
                 }
         }
 
    }
 
    function set_img_thumb_max_width($img_thumb_width)
    {
        $this->set_img_thumb_max_width = $img_thumb_width;
    }
    function set_img_thumb_max_height($img_thumb_height)
    {
        $this->set_img_thumb_max_height = $img_thumb_height;
    }
    function get_mime()
    {
        $img_data = getimagesize($this->img_path);
        $this->mime = $img_data['mime'];
    }
    function image_create()
    {
        switch($this->mime)
        {
            case 'image/jpeg':
                $this->image = imagecreatefromjpeg($this->img_path);
            break;
 
            case 'image/gif':
                $this->image = imagecreatefromgif($this->img_path);
            break;
 
            case 'image/png':
                $this->image = imagecreatefrompng($this->img_path);
            break;
            
            case 'image/bmp':
                $this->image = $this->imagecreatefrombmp($this->img_path);
            break;
        }
    }
function imagecreatefrombmp($p_sFile) {
    $file = fopen($p_sFile, "rb");
    $read = fread($file, 10);
    while (!feof($file) && ($read <> ""))
        $read .= fread($file, 1024);
    $temp = unpack("H*", $read);
    $hex = $temp[1];
    $header = substr($hex, 0, 108);
    if (substr($header, 0, 4) == "424d") {
        $header_parts = str_split($header, 2);
        $width = hexdec($header_parts[19] . $header_parts[18]);
        $height = hexdec($header_parts[23] . $header_parts[22]);
        unset($header_parts);
    }
    $x = 0;
    $y = 1;
    $image = imagecreatetruecolor($width, $height);
    $body = substr($hex, 108);
    $body_size = (strlen($body) / 2);
    $header_size = ($width * $height);
    $usePadding = ($body_size > ($header_size * 3) + 4);
    for ($i = 0; $i < $body_size; $i+=3) {
        if ($x >= $width) {
            if ($usePadding)
                $i += $width % 4;
            $x = 0;
            $y++;
            if ($y > $height)
                break;
        }
        $i_pos = $i * 2;
        $r = hexdec($body[$i_pos + 4] . $body[$i_pos + 5]);
        $g = hexdec($body[$i_pos + 2] . $body[$i_pos + 3]);
        $b = hexdec($body[$i_pos] . $body[$i_pos + 1]);
        $color = imagecolorallocate($image, $r, $g, $b);
        imagesetpixel($image, $x, $height - $y, $color);
        $x++;
    }
    unset($body);
    return $image;
}
    function img_resize_to_thumb()
        {
                set_time_limit(0);
                $this->get_mime();
                $this->image_create();
                $this->img_thumb_width = imagesx($this->image);
                $this->img_thumb_height = imagesy($this->image);
                $this->img_set_dimension();
                $resized_image = imagecreatetruecolor($this->img_new_thumb_width,$this->img_new_thumb_height);
        		if( $this->mime=="image/gif" || $this->mime=="image/png"){
	                imagealphablending($resized_image, false);
			      	imagesavealpha($resized_image,true);
			      	$transparent = imagecolorallocatealpha($resized_image, 255, 255, 255, 127);
			      	imagefilledrectangle($resized_image, 0, 0, $this->img_thumb_width, $this->img_thumb_height, $transparent);
			      
	                imagecopyresampled($resized_image, $this->image, 0, 0, 0, 0, $this->img_new_thumb_width, $this->img_new_thumb_height,$this->img_thumb_width, $this->img_thumb_height);
	                
                }else{
               	 	imagecopyresampled($resized_image, $this->image, 0, 0, 0, 0, $this->img_new_thumb_width, $this->img_new_thumb_height,$this->img_thumb_width, $this->img_thumb_height);
                	imagejpeg($resized_image,$this->img_thumb_save_path);
                }
                 if( $this->mime=="image/gif" ){
                 	imagegif($resized_image,$this->img_thumb_save_path);
                 }else if($this->mime=="image/png"){
                 	imagepng($resized_image,$this->img_thumb_save_path);
                 }
                 else{
                 	imagejpeg($resized_image,$this->img_thumb_save_path);
                 }
 
        }
 
        function img_set_dimension()
        {
 
                if($this->img_thumb_width==$this->img_thumb_height)
                {
                    $case = 'c1';
                }
                elseif($this->img_thumb_width > $this->img_thumb_height)
                {
                    $case = 'c2';
                }
                else
                {
                     $case = 'c3';
                }
 
                if($this->img_thumb_width>$this->set_img_thumb_max_width && $this->img_thumb_height>$this->set_img_thumb_max_height)
                {
                    $cond = 'c1';
                }
                elseif($this->img_thumb_width>$this->set_img_thumb_max_width && $this->img_thumb_height<=$this->set_img_thumb_max_height)
                {
                    $cond = 'c1';
                }
                else
                {
                    $cond = 'c3';
                }
 
                switch($case)
                {
                    case 'c1':
                        $this->img_new_thumb_width = $this->set_img_thumb_max_width;
                        $this->img_new_thumb_height = $this->set_img_thumb_max_height;
                    break;
                    case 'c2':
                        $img_ratio = $this->img_thumb_width/$this->img_thumb_height;
                        $amount = $this->img_thumb_width - $this->set_img_thumb_max_width;
                        $this->img_new_thumb_width = $this->img_thumb_width - $amount;
                        $this->img_new_thumb_height = $this->img_thumb_height - ($amount/$img_ratio);
                    break;
                    case 'c3':
                        $img_ratio = $this->img_thumb_height/$this->img_thumb_width;
                        $amount = $this->img_thumb_height - $this->set_img_thumb_max_height;
                        $this->img_new_thumb_height = $this->img_thumb_height - $amount;
                        $this->img_new_thumb_width = $this->img_thumb_width - ($amount/$img_ratio);
                    break;
                }
 
        }
}
$folder="post";
if(isset($_REQUEST['folder']))
$folder=$_REQUEST['folder'];
$file_path = "images/$folder/"; 
$file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
$file_path_thumbs="thumbs/".$file_path;
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
	
		list($width, $height, $type, $attr) = getimagesize($file_path);
			if( $width >1280 || $height>1280){
				$obj = new image_resize_thumb();
				$obj->set_img_thumb_max_width(1280);
				$obj->set_img_thumb_max_height(1280);
				$obj->image_file($file_path,$file_path);
			}
		$compressed = compress_image($file_path, $file_path, 25);
		$obj = new image_resize_thumb();
		if( $width >320 || $height>320){
					$obj->set_img_thumb_max_width(320);
					$obj->set_img_thumb_max_height(320);
		}else{
					$obj->set_img_thumb_max_width($width);
					$obj->set_img_thumb_max_height($height);
				}
		$obj->image_file($file_path,$file_path_thumbs);
        echo "success";
} else{
        echo "fail";
}


//testing in server
//$obj->image_file_path("/home/yoursitepath/foldername","/home/yoursitepath/foldername/thumbfolder");
 
//localhost testing




?>